﻿using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomPropertyDrawer(typeof(PUEXDynamicAttribute))]
public class PUEXDynamicAttributePropertyDrawer : PropertyDrawer {

	public override float GetPropertyHeight (SerializedProperty property, GUIContent label) 
	{
		return 18f * 2;
	}

	public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) 
	{
		var attrTypeProperty = property.FindPropertyRelative ("type");
		var paramNameProperty = property.FindPropertyRelative ("paramName");
		var param2NameProperty = property.FindPropertyRelative ("param2Name");
		var valueProperty = property.FindPropertyRelative ("value");
		var value2Property = property.FindPropertyRelative ("value2");
		var curveProperty = property.FindPropertyRelative ("curve");

		label = EditorGUI.BeginProperty(position, label, property);
		Rect contentPosition = EditorGUI.PrefixLabel(position, label);

		contentPosition.height = 18f;
		var oldAttrType = (PUEXDynamicAttribute.DynamicAttributeType)attrTypeProperty.enumValueIndex;
		EditorGUI.PropertyField(contentPosition, attrTypeProperty, GUIContent.none);
		var newAttrType = (PUEXDynamicAttribute.DynamicAttributeType)attrTypeProperty.enumValueIndex;
		if (newAttrType != oldAttrType) {
			// reset unused property
			switch (newAttrType) {
			case PUEXDynamicAttribute.DynamicAttributeType.DAT_FIXED:
				param2NameProperty.stringValue = "";
				value2Property.floatValue = 1f;
				curveProperty.animationCurveValue = null;
				break;
			case PUEXDynamicAttribute.DynamicAttributeType.DAT_RANDOM:
				curveProperty.animationCurveValue = null;
				break;
			case PUEXDynamicAttribute.DynamicAttributeType.DAT_CURVED:
				curveProperty.animationCurveValue = AnimationCurve.Linear(0f, -1f, 1f, 1f);
				break;
			}
		}

//		EditorGUI.indentLevel = 1;
//		position = EditorGUI.IndentedRect(position);
		EditorGUIUtility.labelWidth = 7f * 5;
		contentPosition.width = position.width / 6; contentPosition.height = 18f;

		switch ((PUEXDynamicAttribute.DynamicAttributeType)attrTypeProperty.enumValueIndex) {
		case PUEXDynamicAttribute.DynamicAttributeType.DAT_FIXED:
			contentPosition.x = position.x + contentPosition.width * 4; contentPosition.y = position.y + 18f; 
			EditorGUI.PropertyField(contentPosition, valueProperty, new GUIContent("value"));
			contentPosition.x += contentPosition.width;
			EditorGUI.PropertyField(contentPosition, paramNameProperty, new GUIContent(" <--"));
			break;
		case PUEXDynamicAttribute.DynamicAttributeType.DAT_RANDOM:
			contentPosition.x = position.x + contentPosition.width * 2; contentPosition.y = position.y + 18f; 
			EditorGUI.PropertyField(contentPosition, valueProperty, new GUIContent("min"));
			contentPosition.x += contentPosition.width;
			EditorGUI.PropertyField(contentPosition, paramNameProperty, new GUIContent(" <--"));
			
			contentPosition.x += contentPosition.width;
			EditorGUI.PropertyField(contentPosition, value2Property, new GUIContent("max"));
			contentPosition.x += contentPosition.width;
			EditorGUI.PropertyField(contentPosition, param2NameProperty, new GUIContent(" <--"));
			break;
		case PUEXDynamicAttribute.DynamicAttributeType.DAT_CURVED:
			contentPosition.x = position.x; contentPosition.y = position.y + 18f; 
			EditorGUI.PropertyField(contentPosition, valueProperty, new GUIContent("base"));
			contentPosition.x += contentPosition.width;
			EditorGUI.PropertyField(contentPosition, paramNameProperty, new GUIContent(" <--"));

			contentPosition.x += contentPosition.width;
			EditorGUI.PropertyField(contentPosition, value2Property, new GUIContent("map"));
			contentPosition.x += contentPosition.width;
			EditorGUI.PropertyField(contentPosition, param2NameProperty, new GUIContent(" <--"));

			contentPosition.x += contentPosition.width + 7f; contentPosition.width = position.width / 3 - 7f;
			EditorGUI.CurveField(contentPosition, curveProperty, Color.green, Rect.MinMaxRect(0f, -1f, 1f, 1f));
			break;
		}

		EditorGUI.EndProperty();
	}

}
